﻿Imports System.Runtime.CompilerServices
Imports Co9320.NamaAlert.Model

Module ExtensionModule

    <Extension()> _
    Public Function ToModel(ByVal station As Stations, ByVal pluginGuids As IList(Of Guid)) As Station

        Dim s = New Station With { _
            .Id = station.Id, _
            .IsAlertTarget = station.IsAlertTarget, _
            .Name = station.Name, _
            .Rating = station.Rating, _
            .Note = station.Note}

        For Each i In pluginGuids
            s.PluginGuids.Add(i)
        Next

        Return s
    End Function

    <Extension()> _
    Public Function ToModel(ByVal program As LivePrograms, ByVal station As Stations, ByVal pluginGuids As IList(Of Guid)) As LiveProgram

        Debug.Assert(program.StationId = station.Id)

        Dim p = New LiveProgram With { _
            .Id = program.Id, _
            .Title = program.Title, _
            .ShortContent = program.Content, _
            .Content = program.Content, _
            .CasterName = program.Caster, _
            .CommentCount = program.CommentCount, _
            .VisitorCount = program.VisitorCount, _
            .StartDateTime = program.StartDateTime, _
            .OfficialTag = program.OfficialTag, _
            .IsFace = program.IsFace, _
            .IsMemberOnly = program.IsMemberOnly, _
            .CrawlingDateTime = program.CrawlingDateTime}

        With p.Station
            .Id = station.Id
            .IsAlertTarget = station.IsAlertTarget
            .Name = station.Name
            .Rating = station.Rating
            .Note = station.Note
        End With

        For Each i In pluginGuids
            p.Station.PluginGuids.Add(i)
        Next

        Return p
    End Function

    <Extension()> _
    Public Function ToEntity(ByVal station As Station) As Stations

        Dim s = New Stations With { _
            .Id = station.Id, _
            .IsAlertTarget = station.IsAlertTarget, _
            .Name = station.Name, _
            .Rating = CByte(station.Rating), _
            .AlertSettingsGuid = Guid.NewGuid, _
            .Note = station.Note}

        Return s
    End Function

    <Extension()> _
    Public Function ToEntity(ByVal program As LiveProgram) As LivePrograms

        Dim p = New LivePrograms With { _
            .Id = program.Id, _
            .Title = program.Title, _
            .Content = program.Content, _
            .Caster = If(program.CasterName, ""), _
            .CommentCount = program.CommentCount, _
            .VisitorCount = program.VisitorCount, _
            .StartDateTime = program.StartDateTime, _
            .OfficialTag = program.OfficialTag, _
            .IsFace = program.IsFace, _
            .IsMemberOnly = program.IsMemberOnly, _
            .CrawlingDateTime = program.CrawlingDateTime, _
            .StationId = program.Station.Id}

        Return p
    End Function

End Module
